package com.pks.datacenter.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.pks.datacenter.aspect.AspectLogTag;
import com.pks.datacenter.entity.api.ApiParams;
import com.pks.datacenter.entity.basic.DbField;
import com.pks.datacenter.service.api.ApiMethodService;
import com.pks.datacenter.service.api.ApiParamsService;
import com.pks.datacenter.service.basic.DbFieldService;
import com.pks.resultful.common.SRemoteResult;
import com.pks.util.UtilConvert;
import com.pks.util.UtilString;
import com.pks.web.springmvc.controller.BasicController;


@RestController
@RequestMapping(value = "/apiParams")
@Scope("prototype")
@Component("apiParamsController")
@Api(tags = "数据源对外接口参数设置")
public class ApiParamsController extends BasicController {

	@Resource
	private ApiParamsService apiParamsService;
	@Resource
	private ApiMethodService apiMethodService;
	@Resource
	private DbFieldService dbFieldService;

	@AspectLogTag(operType = "删除")
	@ApiOperation(value = "删除记录")
	@RequestMapping(value = "/delete" , method = RequestMethod.POST)
	public SRemoteResult<String> delete(HttpServletRequest request, String ids) {
		if (UtilString.isBlank(ids)) {
			return SRemoteResult.initErrorParamBlank(null);
		}
		if (StringUtils.isBlank(ids) || !ids.matches("^[0-9]+(,[0-9]+)*$")) {
			return SRemoteResult.initErrorParamInvalid(null);
		}
		List<Long> idList = UtilConvert.string2LongList(ids);
		SRemoteResult<String> sr = apiParamsService.deleteApiParams(idList);
		return sr;
	}
	
	@AspectLogTag(operType = "修改")
	@ApiOperation(value = "新增或修改")
	@RequestMapping(value = "/modify" , method = RequestMethod.POST)
	public SRemoteResult<String> modify(HttpServletRequest request, ApiParams obj) {
		if (obj == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		SRemoteResult<String> sr = apiParamsService.insertOrModify(obj);
		return sr;
	}

	@ApiOperation(value = "获取详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "id", dataType = "long" , paramType = "query" )
	})
	@RequestMapping(value = "/get" , method = RequestMethod.GET)
	public SRemoteResult<ApiParams> findInfo(HttpServletRequest request, Long id) {
		if (id == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		ApiParams obj = apiParamsService.findById(id);
		if (obj == null) {
			return SRemoteResult.initErrorDataNone();
		}
		DbField dbField = dbFieldService.findById(obj.getDbFieldId());
		if (dbField != null) {
			obj.setShow_dbField_enName(dbField.getEnName());
		}
		return SRemoteResult.initSuccess(obj);
	}

	@ApiOperation(value = "列表查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "apiId",value = "接口ID", dataType = "Long" , paramType = "query" )
		,@ApiImplicitParam(name = "offset",value = "分页起始页（默认0）", dataType = "int" , paramType = "query" )
		,@ApiImplicitParam(name = "pagesize",value = "分页每页行数（默认10）", dataType = "int" , paramType = "query" )
	})
	@RequestMapping(value = "/list" , method = RequestMethod.POST)
	public SRemoteResult<List<ApiParams>> list(HttpServletRequest request,Long apiId,Long dbId,String paramName ,String paramNameLike ,Long dbFieldId,Integer paramType,Integer requiredState ) {
		Map<String, Object> qbuilder = new HashMap<String, Object>();
		if (apiId != null) {
			qbuilder.put("apiId", apiId);
		}
		if (dbId != null) {
			qbuilder.put("dbId", dbId);
		}
		if (UtilString.isNotBlank(paramName)) {
			qbuilder.put("paramName", paramName);
		}
		if (UtilString.isNotBlank(paramNameLike)) {
			qbuilder.put("paramNameLike", "%" + paramNameLike.trim() + "%");
		}
		if (dbFieldId != null) {
			qbuilder.put("dbFieldId", dbFieldId);
		}
		if (paramType != null) {
			qbuilder.put("paramType", paramType);
		}
		if (requiredState != null) {
			qbuilder.put("requiredState", requiredState);
		}
		List<ApiParams> list = new ArrayList<ApiParams>();
		int total = 0;
		total = apiParamsService.getTotalRows(qbuilder);
		if (total > 0) {
			list = apiParamsService.findByPage(null, qbuilder);
			List<Long> dbFieldIds = new ArrayList<Long>();
 			for (ApiParams obj : list) {
 				dbFieldIds.add(obj.getDbFieldId());
			}
 			List<DbField> dbFieldList = dbFieldService.find(null, dbFieldService.initQbuilder("ids", dbFieldIds), null);
 			Map<Long, DbField> dbFieldMap = new HashMap<Long, DbField>();
 			for (DbField dbField : dbFieldList) {
 				dbFieldMap.put(dbField.getId(), dbField);
			}
			for (ApiParams obj : list) {
				if (dbFieldMap.get(obj.getDbFieldId()) != null) {
					DbField dbField = dbFieldMap.get(obj.getDbFieldId());
					obj.setShow_dbField_enName(dbField.getEnName());
				}
			}
		}
		return SRemoteResult.initPageList(list, total);
	}


//    @ApiOperation(value = "数据查询（不分页）")
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "apiId",value = "接口ID", dataType = "Long" , paramType = "query" )
//    })
//    @RequestMapping(value = "/data" , method = RequestMethod.POST)
//    public SRemoteResult<List<ApiParams>> data(HttpServletRequest request,Long apiId  ) {
//        Map<String, Object> qbuilder = new HashMap<String, Object>();
//        if (apiId != null) {
//            qbuilder.put("apiId", apiId);
//        }
//        List<ApiParams> list = new ArrayList<ApiParams>();
//        list = apiParamsService.find(null, qbuilder, null);
//        return SRemoteResult.initPageList(list, null);
//    }


}
